package com.uenpay.im.ui.old.activity.movement;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.uenpay.im.BroadcastAction;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.chat.ActivityHandle;
import com.uenpay.im.weight.listview.RefreshListView;

import java.util.ArrayList;

/**
 * CopyRight:
 *
 *
 * @author SL
 * @copyRight http://www.qiyunxin.com
 *
 */

public class MyPublishedMovementFragment extends Fragment implements RefreshListView.IRefreshListViewListener {

	private int page = 1;
	private View view;
	private MovementHandler movementHandler = new MovementHandler();
	private RefreshListView listview;
	private MovementListAdapter adapter = null;
	private MovementBroadcastReceiver broadcastReceiver;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (view == null) {
			view = inflater.inflate(R.layout.fragment_xlistview_layout, null);
			initView();
			initListener();
		}
		ViewGroup parent = (ViewGroup) view.getParent();
		if (parent != null) {
			parent.removeView(view);
		}
		getData();
		return view;
	}

	private void initView() {
		listview = (RefreshListView) view.findViewById(R.id.listview);
	}

	private void initListener() {
		listview.setPullLoadEnable(true);
		listview.setRefreshListViewListener(this);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				MovementEntity entity = (MovementEntity) arg0.getAdapter().getItem(arg2);
				if(entity != null){
					Bundle bundle = new Bundle();
					bundle.putInt("movementId", entity.movementId);
					ActivityHandle.startActivity(getActivity(), MovementDetailActivity.class, bundle, -1);
				}
			}
		});
	}

	private void getData() {
		movementHandler.getMovementList(3,page, IMApplication.getCustId(),
				new MovementHandler.IGetMovementListResultListener() {

					@Override
					public void onResult(int status, String msg,
							ArrayList<MovementEntity> arrayList) {
						onLoad();
						if (status == HttpInvokeResultCode.RESULT_OK) {
							if (page == 1) {
								if (adapter == null) {
									adapter = new MovementListAdapter(
											getActivity(), arrayList);
								} else {
									adapter.setData(arrayList);
								}
								listview.setAdapter(adapter);
								listview.setPullLoadEnable(true);
							} else {
								if (arrayList != null && arrayList.size() > 0) {
									adapter.addData(arrayList);
								}else {
									listview.setPullLoadEnable(false);
								}
							}
						}
					}
				});
	}

	private void onLoad() {
		listview.stopRefresh();
		listview.stopLoadMore();
	}

	@Override
	public void onRefresh() {
		page = 1;
		getData();
	}

	@Override
	public void onLoadMore() {
		page++;
		getData();
	}

	@Override
	public void onResume() {
		if (broadcastReceiver == null) {
			broadcastReceiver = new MovementBroadcastReceiver();
			IntentFilter filter = new IntentFilter();
			filter.addAction(BroadcastAction.ACTION_REFRESH_MOVEMENT);
			getActivity().registerReceiver(broadcastReceiver, filter);
		}
		super.onResume();
	}

	@Override
	public void onDestroy() {
		if (broadcastReceiver != null) {
			getActivity().unregisterReceiver(broadcastReceiver);
		}
		super.onDestroy();
	}

	private class MovementBroadcastReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context arg0, Intent arg1) {
			if (arg1.getAction()
					.equals(BroadcastAction.ACTION_REFRESH_MOVEMENT)) {
				page = 1;
				getData();
			}
		}

	};
}
